#include "documentdialog.h"
#include "ui_documentdialog.h"
#include <QVariant>



DocumentDialog::DocumentDialog(Document* doc,Database* db, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DocumentDialog)
{
    document = doc;
    database = db;
    ui->setupUi(this);
    ui->pushButton_type->setText(db->getDocumentType(doc->type));
    ui->pushButton_client->setText(db->getClientName(doc->client));
    ui->spinBox_number->setValue(doc->number);
    ui->dateEdit->setDate(doc->date);
    ui->checkBox_payed->setChecked(doc->payed);
    ui->checkBox_closed->setChecked(doc->closed);
    ui->total->setText(QString().number(doc->total));

    ui->tableWidget->setRowCount(doc->rows.count()+1);
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setColumnHidden(0,true);
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem(tr("Product")));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem(tr("Quantity")));
    ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem(tr("Unit Price")));
    ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem(tr("Amount")));

    int cr = 0;
    QHashIterator<int, DocumentRow*> i(doc->rows);
    while (i.hasNext()) {
        i.next();
        ui->tableWidget->setItem(cr,0, new QTableWidgetItem(QString().number(i.value()->ID)));
        ui->tableWidget->setItem(cr,1, new QTableWidgetItem(db->getProductName(i.value()->product)));
        ui->tableWidget->setItem(cr,2, new QTableWidgetItem(QString().number(i.value()->qty)));
        ui->tableWidget->setItem(cr,3, new QTableWidgetItem(QString().number(i.value()->price)));
        ui->tableWidget->setItem(cr,4, new QTableWidgetItem(QString().number(i.value()->amount)));
        ui->tableWidget->item(cr,0)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget->item(cr,1)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget->item(cr,4)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        cr++;
    }
    addButton = new QPushButton(tr("Add"));
    ui->tableWidget->setCellWidget(cr,1,addButton);
    connect (ui->tableWidget,SIGNAL(cellChanged(int,int)),this,SLOT(tableChanged(int,int)));
    connect (ui->tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(tableClicked(int,int)));
    connect (addButton,SIGNAL(pressed()),this,SLOT(addRow()));
    connect (ui->pushButton_client,SIGNAL(pressed()),this, SLOT(buttonClicked()));
    connect (ui->pushButton_type,SIGNAL(pressed()),this, SLOT(buttonClicked()));

}

void DocumentDialog::tableChanged(int r, int c)
{
    if (c==2||c==3)
    {
        int id = ui->tableWidget->item(r,0)->text().toInt();
        int qty = ui->tableWidget->item(r,2)->text().toInt();
        int price = ui->tableWidget->item(r,3)->text().toDouble();
        document->changeRow(id, document->rows[id]->product,qty,price);
        ui->tableWidget->item(r,4)->setText(QString().number(document->rows[id]->amount));
        ui->total->setText(QString().number(document->total));

    }


}

void DocumentDialog::tableClicked(int r, int c)
{
    if (c==1)
        ui->total->setText(QString().number(r));

   //else ui->total->setText("0");
}

void DocumentDialog::addRow()
{
   ui->tableWidget->insertRow(this->document->rows.count());

   //else ui->total->setText("0");
}

void DocumentDialog::buttonClicked()
{
    QPushButton *button = (QPushButton *)sender();
    qDebug() << button->text() <<button->objectName();
 //   emit digitClicked(button->text()[0].digitValue());
}

DocumentDialog::~DocumentDialog()
{
    delete ui;
}
